package org.springcloud.wisdom.spring_01_canteen.dishes.cart;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class CartServiceImpl extends ServiceImpl<CartDao, Cart> implements CartService {
    //添加到购物车
    @Override
    @Transactional
    public void addToCart(Cart cart) {
        cart.calculateTotalPrice();
        this.save(cart);
    }

    //更改数量
    @Override
    @Transactional
    public boolean updateCartQuantity(Integer id, Integer newCount) {
        Cart cart = this.getById(id);
        if (cart != null) {
            cart.setCount(newCount);
            cart.calculateTotalPrice();
            this.updateById(cart);
            return true;
        }
        return false;
    }

    //删除商品
    @Override
    @Transactional
    public void removeFromCart(Integer cartId) {
        this.removeById(cartId);
    }

    // 清空购物车
    @Override
    @Transactional
    public void clearCart() {
        // 删除 cart 表中的所有记录
        // null 表示没有条件，删除所有记录
        this.remove(null);
    }


}
